var groupObj = {
    userid: "",
    lTeamId: "",
    init: function() {
        this.userid = $.cookie('userid');
        this.lTeamId = publicObj.getUrlData("lTeamId");
        this.getGroupInfor();
        this.joinGroup();
        this.tooDetail();
    },
    //拼团信息
    getGroupInfor: function() {
        var me = this;
        var url = magic_config.urlPrefix + 'GroupbuyService/getallgroupbuyinfo';
        var sUserIdStr;
        if (me.userid == null) {
            sUserIdStr = "";
        } else {
            sUserIdStr = me.userid;
        }
        var data = {
            lTeamId: me.lTeamId,
            sUserId: sUserIdStr
        }
        console.log(data);
        $.ajax({
            url: url,
            type: 'get',
            data: data,
            async: false,
            dataType: 'json',
            success: function(res) {
                var nCode = res.return.nCode;
                if (nCode === 0) {
                    //console.log(res.response);
                    me.splitGroupinfor(res.response);
                    me.splitDetail(res.response.cdoData)
                } else {
                    console.log("数据请求失败，后到前");
                }
            },
            error: function(res) {
                console.log("数据请求失败，前到后");
            }
        });
    },
    splitGroupinfor: function(resObj) {
        var str = '';
        var groupList = resObj.cdoDataList;
        var groulpCount = resObj.cdoData.nCountPerGroup;
        console.log(resObj);
        for (var i = 0; i < 5; i++) {
            if (i == 0) {
                str += '<div class="userimgwall"><div class="userimg"><img src="' + groupList[i].sUserPhoto + '" alt="" /></div><div class="leader">拼主</div></div>';
            } else {
                if (i < groupList.length) {
                    str += '<div class="userimgwall"><div class="userimg"><img src="' + groupList[i].sUserPhoto + '" alt="" /></div></div>';
                } else {
                    str += '<div class="userimgwall"><div class="userimg userimg2"><img src="./img/himg.png" alt=""></div></div>';
                }
            }
        }
        $('.userimgbox').html(str);
        //头部信息
        var remainNum = groulpCount - groupList.length;
        $(".groupsug").html('还差<span> ' + remainNum + '</span> 人拼团成功，剩余时间<span class="countdown"> -- : -- : --</span>');
        $(".groupnum").html('<span>' + groulpCount + '</span>人拼团');
        $(".s_num").html(remainNum);
    },
    splitDetail: function(goodsDetail) {
        //商品名字
         $(".goodsname").html('【'+goodsDetail.sCorpName+'】'+goodsDetail.sName);
        //商品价格
        $(".goodsprc").html('<span>¥</span>' + goodsDetail.nGroupPrice);
        $(".goodsoprc").html("¥" + goodsDetail.nMainPrice);
        $(".ptgz").html('拼团成功后，获得<span>拼团券</span>、去' + goodsDetail.sCorpName + '<span>【扫码购】</span>使用')
        //商品图片
        $(".goodsimg").html('<img src="' + goodsDetail.sHeadPicture + '" alt="" />');
        console.log(goodsDetail);
        this.getActiveEndTime(goodsDetail.ptLoseEffTime);
    },
    //倒计时
    getActiveEndTime: function(endTimeStr) {
        endTimeStr = endTimeStr.replace(/-/g, '/');
        var me = this;
        var interval = setInterval(function() {
            var str = '';
            var endTime = (new Date(endTimeStr)).getTime();
            var date = new Date();
            var nowTime = date.getTime();
            endTime = parseInt(endTime / 1000);
            countdownTime = endTime - parseInt(nowTime / 1000);
            var day;
            var hour;
            var min;
            var sec;
            var allhour;
            day = parseInt(countdownTime / (3600 * 24));
            hour = parseInt((countdownTime - day * (3600 * 24)) / 3600);
            allhour = parseInt(countdownTime / 3600);
            min = parseInt((countdownTime - day * (3600 * 24) - hour * 3600) / 60);
            sec = countdownTime - day * (3600 * 24) - hour * 3600 - min * 60;
            if (sec <= 0 && min <= 0 && hour <= 0) {
                sec = "00";
                min = "00";
                hour = "00";
                allhour = "00";
            } else {
                if (Number(sec) < 10) {
                    sec = "0" + sec;
                }
                if (Number(min) < 10) {
                    min = "0" + min;
                }
                if (Number(hour) < 10) {
                    hour = "0" + hour;
                }
                if (Number(allhour) < 10) {
                    allhour = "0" + allhour;
                }
            }
            var a = '<span class="c-size">' + allhour + '</span> : <span class="c-size">' + min + '</span> : <span class="c-size">' + sec + '</span>';
            $(".countdown").html(a);
        }, 1000);
    },
    //查看商品详情
    tooDetail: function() {
        var me = this;
        $(".groupinforbox").bind("click", function() {
            location.href = 'detail_h5.html?lTeamId=' + me.lTeamId;
        })
    },
    //立即参团
    joinGroup: function() {
        var me = this;
        $(".intverfs").bind("click", function() {
            console.log(me.userid);
            if (me.userid == null) {
                $(".layerbox").show();
                $(".normallogin").show();
            } else {

            }
        })
    }
}
groupObj.init();